/**
 * 
 */
package com.jeedle.nahe;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import android.R.layout;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.opengl.Visibility;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Toast;

/**
 * @author maximo
 *
 */
public class OpenFileDialog extends Activity {

	private String m_chosenDir = "";
    private boolean m_newFolderEnabled = false;
    
	 @Override
	 public void onCreate(Bundle savedInstanceState)
	 {
		 m_chosenDir = MainActivity.starting_folder;
		 
		    super.onCreate(savedInstanceState);
			setVisible(false);    		    
		       FileChooserDialog directoryChooserDialog = 
		                new FileChooserDialog(OpenFileDialog.this, 
		                    new FileChooserDialog.ChosenDirectoryListener() 
		                {
		                    @Override
		                    public void onChosenDir(String chosenDir) 
		                    {
		                        m_chosenDir = chosenDir;
		                        Toast.makeText(
		                        		OpenFileDialog.this, "Chosen file: " + 
		                          chosenDir.substring(chosenDir.lastIndexOf('/', chosenDir.length())).replace("/", ""), Toast.LENGTH_LONG).show();
		                        
		                        String filepath = m_chosenDir;
		                        
		                        try
		                        {
			   		    			 FileInputStream in = new FileInputStream(new File(filepath));
			   			    		 InputStreamReader inputStream = new InputStreamReader(in);
			   			    		 BufferedReader reader = new BufferedReader(inputStream);
			   			    		 StringBuilder sb = new StringBuilder();
			   			    		 String line;
			   			    		 while ((line = reader.readLine()) != null)
			   			    		 {
			   			    			 sb.append(line);
			   			    		 }
	
			   			    		 in.close();
			   			    		 inputStream.close();
			   			    		 reader.close();
			   			    		 
			   			    		 filepath = filepath.substring(0,filepath.lastIndexOf('/', filepath.length()));
			   			    		 Intent resIntent = new Intent();
			   			    		 resIntent.putExtra("fileText", sb.toString());
			   			    		 resIntent.putExtra("FilePath",filepath);
			   			    		 setResult(MainActivity.PICK_FILE_NAME,resIntent);
		   		    		     }
		   		    		 catch (IOException e)
			   		    		 {
			   		    			 e.printStackTrace();
			   		    		 }

		                    }
		                }); 
		       
	            // Toggle new folder button enabling
               directoryChooserDialog.setNewFolderEnabled(m_newFolderEnabled);
               // Load directory chooser dialog for initial 'm_chosenDir' directory.
               // The registered callback will be called upon final directory selection.
               directoryChooserDialog.chooseDirectory(m_chosenDir);
               directoryChooserDialog.getDialog().setOwnerActivity(this);
		   
               m_newFolderEnabled = ! m_newFolderEnabled;		   
	 }
		 
}
